package pers.mihao.quickstream.common.task.service.impl;

import org.springframework.stereotype.Component;
import pers.mihao.quickstream.common.model.result.Result;
import pers.mihao.quickstream.common.task.dto.AutomaticTaskDTO;
import pers.mihao.quickstream.common.task.dto.UniqueBizInfo;
import pers.mihao.quickstream.common.task.manager.AutomaticTaskManager;
import pers.mihao.quickstream.common.task.repository.AutomaticTaskRepository;
import pers.mihao.quickstream.common.task.service.AutomaticTaskService;
import pers.mihao.quickstream.common.task.dto.JavaAutomaticTaskDTO;
import pers.mihao.quickstream.common.util.AssertUtil;

import javax.annotation.Resource;

/**
 * @author mihao
 * @since 2024/11/14
 */
@Component
public class AutomaticTaskServiceImpl implements AutomaticTaskService {

    @Resource
    private AutomaticTaskRepository automaticTaskRepository;

    @Resource
    private AutomaticTaskManager automaticTaskManager;

    @Override
    public Result<Void> createJavaAutomaticTask(JavaAutomaticTaskDTO<?> javaTaskDTO) {
        AssertUtil.isTrue(javaTaskDTO != null);
        AssertUtil.isTrue(javaTaskDTO.getUniqueBizInfo() != null);
        UniqueBizInfo uniqueBizInfo = javaTaskDTO.getUniqueBizInfo();

        AutomaticTaskDTO automaticTaskDTO = automaticTaskRepository.queryByBizId(uniqueBizInfo);
        if (automaticTaskDTO != null) {
            return Result.fail("当前bizId已经创建" + javaTaskDTO.getUniqueBizInfo().getBizId());
        }
        automaticTaskManager.saveAutomaticTask(javaTaskDTO);
        return Result.create(true);
    }
}
